
var headStr = '';
var appId='';
var isProductEnv=false;//是否是正式环境
if (isProductEnv){
    headStr = 'yc';
} else {
  //本地测试
  headStr = 'https://px.diandianja.com/weiloupan/'//localhost:8080/manager/'; //'http://localhost:8080/manager/';
//headStr = 'https://xnzx.diandianja.com/manager/'//localhost:8080/manager/'; //'http://localhost:8080/manager/';

   //headStr = 'https://xnzx.diandianja.com/manager/'//localhost:8080/manager/'; //'http://localhost:8080/manager/';
  //headStr = 'http://192.168.1.80:8080/manager/';
  //headStr = 'http://localhost:8081/manager/';

 // headStr = 'https://px.diandianja.com/weiloupan/' 
  //headStr = 'https://xnzx.diandianja.com/manager/'
}

var ajaxPost = function(url, object, successback) {
  // object['appId']=appId;
  wx.request({
    url: headStr+url, //仅为示例，并非真实的接口地址
    method: 'POST',
    data: object,
    header: {
      'content-type': 'application/json'// 默认值
    },
    success: function (res) {
      if (!(res.data && res.data.result) && res.data.resultMsg){
        if (res.data.resultCode != '22222'){
          wx.showToast({
            title: res.data.resultMsg,
            icon: 'none',
            duration: 2000
          })
        }
      }
      successback(res.data);
    },
    fail: function (res){
      successback();
    }
  })
}



var ajaxPostNo = function (url, object, successback) {
  // object['appId']=appId;
  wx.request({
    url: headStr + url, //仅为示例，并非真实的接口地址
    method: 'POST',
    data: object,
    header: {
      'content-type': 'application/json'// 默认值
    },
    success: function (res) {
      // if (!(res.data && res.data.result) && res.data.resultMsg) {
      //   if (res.data.resultCode != '22222') {
      //     wx.showToast({
      //       title: res.data.resultMsg,
      //       icon: 'none',
      //       duration: 2000
      //     })
      //   }
      // }
      successback(res);
    },
    fail: function (res) {
      successback();
    }
  })
}

var ajaxGet = function (url, object, successback) {
  wx.request({
    url: headStr + url, //仅为示例，并非真实的接口地址
    method: 'GET',
    data: object,
    header: {
      'content-type': 'application/json'// 默认值
    },
    success: function (res) {
      if (!(res.data && res.data.result) && res.data.resultMsg) {
        wx.showToast({
          title: res.data.resultMsg,
          icon: 'none',
          duration: 2000
        })
      }
      successback(res.data);
    },
    fail: function (res) {
      successback();
    }
  })
}
/**
 * 百度坐标系 (BD-09) 与 火星坐标系 (GCJ-02)的转换
 * 即 百度 转 谷歌、高德
 * @param bd_lon
 * @param bd_lat
 * @returns {*[]}
 */
function bd09togcj02(bd_lon, bd_lat) {
  var x_pi = 3.14159265358979324 * 3000.0 / 180.0;
  var x = bd_lon - 0.0065;
  var y = bd_lat - 0.006;
  var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
  var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
  var gg_lng = z * Math.cos(theta);
  var gg_lat = z * Math.sin(theta);
  return [gg_lng, gg_lat]
}

function openPosition(lat, lon,name,address) {
  console.log(lat + '.....' + lon + '.....' + name + '.....' + address)
  wx.openLocation({ //出发wx.openLocation API
    latitude: lat,
    longitude: lon,
    name:name, //打开后显示的地址名称
    address:address //打开后显示的地址汉字
  })
}

//验证电话号码
function check_phone(phoneNum) {
  return /^1[0-9]{10}$/.test(phoneNum);//true
}

function addBuildingPV(loupanId){
  wx.request({
    url: headStr +"portal/wlpBuilding/addBuildingPV.action", //仅为示例，并非真实的接口地址
    method: 'POST',
    data: {
      id: loupanId
    },
    header: {
      'content-type': 'application/json'// 默认值
    },
    success: function (res) {
      if (!(res.data && res.data.result) && res.data.resultMsg) {
        wx.showToast({
          title: res.data.resultMsg,
          icon: 'none',
          duration: 2000
        })
      }
    },
    fail: function (res) {
      
    }
  })
}


function addPvNum(loupanId) {
  wx.request({
    url: headStr + "portal/wlpAdvisory/addPvNum.action", //仅为示例，并非真实的接口地址
    method: 'POST',
    data: {
      id: loupanId
    },
    header: {
      'content-type': 'application/json'// 默认值
    },
    success: function (res) {
      if (!(res.data && res.data.result) && res.data.resultMsg) {
        wx.showToast({
          title: res.data.resultMsg,
          icon: 'none',
          duration: 2000
        })
      }
    },
    fail: function (res) {

    }
  })
}


function queryGoodsByAppId(appId,callback) {
  var params = {
    appId: appId
  };
  wx.request({
    url: headStr + "portal/wlpGoods/queryGoodsByAppId.action",
    method: 'POST',
    data: JSON.stringify(params),
    header: {
      'content-type': 'application/json'// 默认值
    },
    success: function (res) {
        callback(res.data);
        // app.globalData.xcxtype = res.list;
   
    }
  });
}
module.exports = {
  ajaxPost: ajaxPost,
  ajaxGet: ajaxGet,
  bd09togcj02: bd09togcj02,
  openPosition: openPosition,
  checkphone:check_phone,
  ajaxPostNo: ajaxPostNo,
  headStr: headStr,
  addBuildingPV: addBuildingPV,
  addPvNum:addPvNum,
  queryGoodsByAppId: queryGoodsByAppId
};


